/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JInternalFrameGenerarTablaPosiciones.java
 *
 * Created on 07/11/2009, 10:03:32
 */

package com.grandt.view.ventanas.internas;

import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.table.DefaultTableModel;

import com.grandt.controller.ventanas.internas.ControllerGenerarTablaPosiciones;
import com.grandt.model.beans.equipo.Equipo;
import com.grandt.model.exceptions.BuscarDatosException;
import com.grandt.model.manager.ModelGranDT;
import com.grandt.model.manager.Observer;

/**
 *
 * @author jhonny
 */
public class JInternalFrameGenerarTablaPosiciones extends javax.swing.JInternalFrame implements Observer{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1772580406333746200L;
	
	private ControllerGenerarTablaPosiciones	controller;
	/** Creates new form JInternalFrameGenerarTablaPosiciones 
	 * @param model */
    public JInternalFrameGenerarTablaPosiciones(ModelGranDT model) {
        initComponents();
        controller = new ControllerGenerarTablaPosiciones(model,this);
        model.registrarObserverJugadores(this);
    }

    @Override
    public void setVisible(boolean b) {
    	if(b){
    		try {
				controller.initComoponentView();
			} catch (BuscarDatosException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	super.setVisible(b);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMain = new javax.swing.JPanel();
        label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableEquipos = new javax.swing.JTable();
        buttonOk = new javax.swing.JButton();
        ResourceBundle resourceMap = ResourceBundle.getBundle("com/grandt/view/ventanas/internas/resources/JInternalFrameGenerarTablaPosiciones",Locale.getDefault());
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        panelMain.setName("panelMain"); // NOI18N

        label.setText(resourceMap.getString("label.text")); // NOI18N
        label.setName("label"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableEquipos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Puntos"
            }
        ) {
            /**
			 * 
			 */
			private static final long serialVersionUID = -7812672968555950826L;
			Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableEquipos.setName("tableEquipos"); // NOI18N
        jScrollPane1.setViewportView(tableEquipos);

        buttonOk.setText(resourceMap.getString("buttonOk.text")); // NOI18N
        buttonOk.setName("buttonOk"); // NOI18N
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addContainerGap(287, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMainLayout.createSequentialGroup()
                .addContainerGap(339, Short.MAX_VALUE)
                .addComponent(buttonOk)
                .addContainerGap())
        );
        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonOk)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
            .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {
    	controller.ok();
    }//GEN-LAST:event_buttonOkActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonOk;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label;
    private javax.swing.JPanel panelMain;
    private javax.swing.JTable tableEquipos;
    // End of variables declaration//GEN-END:variables
	public void initEquipos(List<Equipo> generarTablaPosiciones,String fecha) {
		clearAllRows();
		for (Equipo equipo : generarTablaPosiciones) {
			((DefaultTableModel)tableEquipos.getModel()).addRow(equipo.toVector(fecha));
		}
	}

	private void clearAllRows() {
		int rows = tableEquipos.getRowCount();
		for (int i = 0; i < rows; i++) {
			((DefaultTableModel)tableEquipos.getModel()).removeRow(0);
		}
		
	}

	@Override
	public void actualizar() {
		try {
			if(isVisible()){
				controller.initComoponentView();
			}
		} catch (BuscarDatosException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
